<?
if (preg_match("/".basename(__FILE__)."/", $_SERVER["PHP_SELF"]))
        die("<b>Error 502:</b> Access denied.");

if ($GLOBALS['configLoaded']!==true)
{
    require_once(dirname(__FILE__) ."/../../modules/common/inc.init.php");
}

// Starting the session manager
$sessionManager = new SessionManager();

// Creating database conetion
$dbLink = connectDb();

// Starting user session
$user = new adminUser();
$user->setDbLink($dbLink);
$user->readSession();

// Creating Lang object
$lang = new adminLang();

// Proceed with non validated users
$loginFormUrl = $GLOBALS["CONF"]["url"]["admin"] ."/login.php";
if(!$user->isValidated || $user->active!='Y')
{
   if(strpos($loginFormUrl, $_SERVER["PHP_SELF"])===false)
   {
       header("location: ". $loginFormUrl);
       exit();
   }
}


// We ALWAYS have to load admin module first
$adminModule = new Module("admin");


// We validate IP address access for users
if (is_array($adminModule->allowNetworkAccess))
{
    // add the ip from a given domain to the valid ip addresses
    //$arDNS = dns_get_record("adverit.com");
    //$ipDNS = $arDNS[0]['ip'];
    //array_push($GLOBALS["CONF"]["ips"], $ipDNS);
    
    $isValidNetworkAccess = false;
    foreach ($adminModule->allowNetworkAccess as $ip)
    {
        if(strpos($_SERVER['REMOTE_ADDR'], $ip) !== false)
        {
            $isValidNetworkAccess = true;
            break;
        }
    }
    if (!$isValidNetworkAccess) 
    {
        header("Status: 403");
        exit("Access Forbidden!");
    }
}
